// @ts-nocheck
import {newsTechnologyData} from '../../../common/newsData.js'
import router from '@system.router';
import prompt from '@system.prompt'
import app from '@system.app'
import continuationRegisterManager from '@ohos.ability.continuationRegisterManager';
import { jsCallJavaAbility } from '../../../common/JsCallJavaAbilityUtils.js';
export default {
    data: {
        index:0,
        newsData:{
            titleDetail:'',
            img2:'',
            author:'',
            time:'',
            img:'',
            content:'',
        },
        text: "",
        iskey:0,


        continueAbilityData:{
            cindex:0,
            cnewsData:{
                titleDetail:'',
                img2:'',
                author:'',
                time:'',
                img:'',
                content:'',
            },
        },
//        token:'',
    },
    onInit(){
        if(this.chooseId){
            this.index=this.chooseId;
        }
        if(this.data1){
            //            this.continueAbilityData.newsData=this.data1;
            this.newsData.titleDetail=this.data1[this.chooseId].titleDetail;
            this.newsData.img2=this.data1[this.chooseId].img2;
            this.newsData.author=this.data1[this.chooseId].author;
            this.newsData.time=this.data1[this.chooseId].time;
            this.newsData.img=this.data1[this.chooseId].img;
            this.newsData.content=this.data1[this.chooseId].content;

        }
        //        console.info(this.chooseId);
        //        console.info(JSON.stringify(this.continueAbilityData));
        //        Object.assign(this.continueAbilityData, this.shareData);


    },
    leftClick(){
        router.back();
    },
    reportClick(){

/*        //要传递的数据
        var data1 = this.newsData.content;
        //action对象，相当于intent
        var action = {};

        //添加service所在的包名
        action.bundleName = 'com.example.smartmirror2';

        //添加service的类名
        action.abilityName = 'com.example.smartmirror2.ReportServiceAbility';

        //添加messageCode ，用于区分其他的action
        action.messageCode = 99;

        //添加数据
        action.data = data1;

        //跳转的activity的类型，0代表service
        action.abilityType = 0;

        //最重要的一步：跳转
        var str = await FeatureAbility.callAbility(action);

        //下面是处理 java FA 返回的数据
        *//*         var result = JSON.parse(str);
        console.log(result.id);
        console.log(result.name);*/
        console.info('语音播报');
    },
    skip(){
        // 应用进行迁移


        let actionData = {
            index:this.index,
            newsData:this.newsData,
        };
        let target = {
//            deviceId:'',
            bundleName: "com.example.smartmirror2",
            abilityName: "com.example.smartmirror2.MainAbility",
            data: actionData,
            url:"pages/homepage/newsDetail/newsDetail"
        };
        let result = FeatureAbility.startAbility(target);

    },
/*    voice:async function(){
        //要传递的数据
        var data2="js调用java" ;
        //action对象，相当于intent
        var action = {};

        //添加service所在的包名
        action.bundleName = 'com.example.smartmirror2';

        //添加service的类名
        action.abilityName = 'com.example.smartmirror2.AsrAbility';

        //添加messageCode ，用于区分其他的action
        action.messageCode = 101;

        //添加数据
        action.data = data2;

        //跳转的activity的类型，0代表service
        action.abilityType = 0;

        //最重要的一步：跳转
        var str = await FeatureAbility.callAbility(action);

        console.info("语音识别");
    },*/
/*    start() {
        jsCallJavaAbility.callAbility("ControllerAbility",100,{}).then(result=>{
            console.log(result)
        })
        console.info("开始")
    },
    stop() {
        jsCallJavaAbility.callAbility("ControllerAbility",101,{}).then(result=>{
            console.log(result)
        })
        jsCallJavaAbility.unSubAbility("ControllerAbility",201).then(result=>{
            if (result.code == 200) {
                console.log("取消订阅成功");
            }
        })
        console.info("关闭订阅")
    },
    sub() {

        jsCallJavaAbility.subAbility("ControllerAbility", 200, (data) => {
            let text = data.data.text
            text && (this.text += text)
            console.info(text)
            if(this.text.indexOf("看新闻")>-1){

//                this.iskey=1;
                this.text=this.text.slice(0,this.text.length-3);
                this.skip2();
                console.info("新闻流转")

            }
            if(this.text.indexOf("读新闻")>-1){
//                this.iskey=2;
                this.text=this.text.slice(0,this.text.length-3);
                this.reportClick();
                console.info("读新闻")
            }
*//*            switch(this.iskey){
                case 1:
                    this.skip2();
                    this.iskey=0;
                    break;
                case 2:
                    this.reportClick();
                    this.iskey=0;
                    break;
            }*//*

        }).then(result => {
            if (result.code == 200) {
                console.log("订阅成功");
            }
        })

    },*/
    skip2(){
        //                console.info("---------------------------------迁移");
//                this.iskey=false;
                let result =  FeatureAbility.continueAbility();
                console.info("result:" + JSON.stringify(result));
                        let continuationExtraParams = {
                    devTypes: ["00E"]
                }
                continuationRegisterManager.register("com.example.smartmirror2", continuationExtraParams, (error, data) => {
                    if (error) {
                        console.error('Operation failed. Cause: ' + error);
                        return;
                    }
                    console.info('Operation succeeded: ' + data);
                    this.token=data;
                })
    },
    onStartContinuation() {
        // 判断当前的状态是不是适合迁移
        //        console.info("onStartContinuation");
        return true;
    },
    onCompleteContinuation(code) {
        // 迁移操作完成，code返回结果
        //        console.info("CompleteContinuation: code = " + code);
    },
    onSaveData(saveData) {
        // 数据保存到savedData中进行迁移。
        this.continueAbilityData.cindex=this.index;
        this.continueAbilityData.cnewsData=this.newsData;
        Object.assign(saveData, this.continueAbilityData);
        console.info('---------------------保存迁移数据');
        //        console.info(JSON.stringify(saveData));

    },
    onRestoreData(restoreData) {
        console.info('收到迁移数据，恢复。');
        this.continueAbilityData = restoreData;
        this.index=this.continueAbilityData.cindex;
        this.newsData=this.continueAbilityData.cnewsData;

        //        console.info(JSON.stringify(this.continueAbilityData));
    },



}